package com.aisile.common.web.wemedia;

import com.aisile.model.admin.pojos.AdUser;
import com.aisile.model.common.dtos.ResponseResult;
import com.aisile.model.common.enums.AdminHttpCodeEnum;
import com.aisile.model.media.pojos.WmUser;
import com.aisile.utils.common.AppJwtUtil;
import com.aisile.utils.threadlocal.AdminThreadLocalUtils;
import com.aisile.utils.threadlocal.WmThreadLocalUtils;
import com.alibaba.fastjson.JSON;
import io.jsonwebtoken.Claims;
import org.springframework.web.filter.GenericFilterBean;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Auther:yry
 * @Date:2023/12/26 0026
 * @VERSON:1.0
 */
@WebFilter(filterName = "wemediaTokenFilter",urlPatterns = "/*")
public class WemediaTokenFilter extends GenericFilterBean {



    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest)servletRequest;
        HttpServletResponse response = (HttpServletResponse)servletResponse;
        ResponseResult responseResult = null;
        try {
            // 放行
            if(request.getRequestURI().contains("/login/in")){
                filterChain.doFilter(request,response);
            }else{
                // 获取token
                String token =  request.getHeader("token");
                if(token == null || "".equals(token.trim())){
                    //直接拦截
                    responseResult = ResponseResult.setAdminHttpCodeEnum(AdminHttpCodeEnum.TOKEN_INVALID);
                }else{
                    // 解析
                    Claims claims =  AppJwtUtil.getClaimsBody(token);
                    // -1 1 0 2   1 2 失败
                    int i =  AppJwtUtil.verifyToken(claims);
                    if(i>0 || i<-1){
                        responseResult = ResponseResult.setAdminHttpCodeEnum(AdminHttpCodeEnum.TOKEN_EXPIRE);
                    }else{
                        // 有效  获取用户id  保存
                        Integer id =  (Integer) claims.get("id");
                        WmUser wmUser = new WmUser();
                        wmUser.setId(id);
                        // 保存用户信息
                        WmThreadLocalUtils.setUser(wmUser);
                        if(i == 0){
                            // 续签  "token"
                            //"REFRESH_TOKEN"  // 默认新生成一个1个小时有效期的token串
                            response.setHeader("REFRESH_TOKEN",AppJwtUtil.getToken(id.longValue()));
                        }
                        // 放行
                        filterChain.doFilter(request,response);
                    }
                }
            }
        }catch (Exception e){
            responseResult =  ResponseResult.setAdminHttpCodeEnum(AdminHttpCodeEnum.SERVER_ERROR);
        }finally {
            // 拦截返回具体内容
            response.setContentType("application/json");
            response.setCharacterEncoding("utf-8");
            if(responseResult != null){
               response.getOutputStream().write(JSON.toJSONString(responseResult).getBytes());
            }
        }

    }
}
